/* ======================================================================
**  CFuzzyTableView.cpp
** ======================================================================
**
** ======================================================================
**  Copyright (c) 2007 by Max Schlee
** ======================================================================
*/
#include <QtGui>
#include "FuzzyTableView.h"

class FuzzySheet;

// ----------------------------------------------------------------------
CFuzzyTableView::CFuzzyTableView(bool fromLoad/*=false*/, QWidget* parent/*=0*/)
	: QTableView(parent)
	, m_pcmnu(0)
	, m_table(0)
	, m_pFuzzyRulesForm(0)
{
	//setObjectName(QString::fromUtf8("FuzzyTable"));
	//QModelIndex index = m_table.index(0,0);
	
	initWindow();
	
	if (fromLoad == false)
	{
		m_table = new FuzzyTableModel(200,200);
		setModel(m_table);
		show();
		qDebug() << (m_table->data(m_table->index(3,3),Qt::DisplayRole)).toInt();
	}
}

// ----------------------------------------------------------------------
CFuzzyTableView::~CFuzzyTableView()
{
	if (m_table != 0)
	{
		delete m_table;
		m_table = 0;
	}
	
	if (m_pFuzzyRulesForm != 0)
	{
		delete m_pFuzzyRulesForm;
		m_pFuzzyRulesForm = 0;
	}
	
	if (m_pcmnu != 0)
	{
		delete m_pcmnu;
		m_pcmnu = 0;
	}
}

// ----------------------------------------------------------------------
void CFuzzyTableView::slotLoad()
{
    QString str = QFileDialog::getOpenFileName();
    if (str.isEmpty())
    {
        return;
    }

    QFile file(str);
    if (file.open(QIODevice::ReadOnly))
    {
        QDataStream stream(&file);
        stream.setVersion(QDataStream::Qt_4_3);
        
        int r = 0; // rows amount
        int c = 0; // columns amount
        stream >> r >> c;
        m_table = new FuzzyTableModel(r,c);
        QVariant var;
        for (int i=0; i<r; i++)
        	for (int j=0; j<c; j++)
        	{
        		stream >> var;        	
        		m_table->setData(m_table->index(i,j),var,Qt::EditRole);
        	}
        file.close();

        m_strFileName = str;
        emit changeWindowTitle(m_strFileName);
    }
    
    setModel(m_table);
    show();
}

// ----------------------------------------------------------------------
void CFuzzyTableView::slotSave()
{
    if (m_strFileName.isEmpty()) 
    {
        slotSaveAs();
        return;
    }

    QFile file(m_strFileName);
    if (file.open(QIODevice::WriteOnly)) 
    {
        QDataStream stream(&file);
        stream.setVersion(QDataStream::Qt_4_3);
        
        //m_table.setData(m_table.index(1,1),QVariant("fuck"),Qt::EditRole);
        QModelIndex index;
        int r = m_table->rowCount(index);	// rows amount
        int c = m_table->columnCount(index);	// columns amount
        stream << r << c;
        for (int i=0; i<r; i++)
        	for (int j=0; j<c; j++)
        		stream << m_table->data(m_table->index(i,j),Qt::DisplayRole);
        file.close();
        emit changeWindowTitle(m_strFileName);
    }
}

// ----------------------------------------------------------------------
void CFuzzyTableView::slotSaveAs() 
{
    QString str = QFileDialog::getSaveFileName(0, m_strFileName);
    if (!str.isEmpty()) {
        m_strFileName = str;
        slotSave();
    }
}

// ----------------------------------------------------------------------
void CFuzzyTableView::initWindow()
{
	//CONTEXT MENU
	/////////////////////////////////////////////////////////////////////
	
	QAction* pactRules = new QAction("Set rules...", 0);
	pactRules->setText("Set &rules...");
	pactRules->setShortcut(QKeySequence("CTRL+R"));
	pactRules->setToolTip("Set linguistic rules in graphical mode");
	pactRules->setStatusTip("Set linguistic rules in graphical mode");
	pactRules->setWhatsThis("Set linguistic rules for selected model in graphical mode");
    //pactRules->setIcon(QPixmap(filenew));
    connect(pactRules, SIGNAL(triggered()), SLOT(slotSwitchGraphsView()));

    m_pcmnu = new QMenu(this);
    m_pcmnu->addAction(pactRules);
    //m_pcmnu->addSeparator();

    /*
    QToolBar* ptbFile = new QToolBar("File Operations");
    ptbFile->addAction(pactNew);
    ptbFile->addAction(pactOpen);
    ptbFile->addAction(pactSave);
    addToolBar(Qt::TopToolBarArea, ptbFile);
    */
    //(FuzzySheet*)parent()

/*
    menuBar()->addMenu(pmnuFile);
    m_pmnuWindows = new QMenu("&Windows");
    menuBar()->addMenu(m_pmnuWindows);
    connect(m_pmnuWindows, SIGNAL(aboutToShow()), SLOT(slotWindows()));
    menuBar()->addSeparator();

    QMenu* pmnuHelp = new QMenu("&Help");
    pmnuHelp->addAction("&About", this, SLOT(slotAbout()), Qt::Key_F1);
    menuBar()->addMenu(pmnuHelp);

    m_pws = new QWorkspace;
    m_pws->setScrollBarsEnabled(true);
    setCentralWidget(m_pws);

    m_psigMapper = new QSignalMapper(this);
    connect(m_psigMapper, 
            SIGNAL(mapped(QWidget*)), 
            m_pws, 
            SLOT(setActiveWindow(QWidget*))
           );

    statusBar()->showMessage("Ready", 3000);*/
}

void CFuzzyTableView::contextMenuEvent(QContextMenuEvent* pe)
{
	m_pcmnu->exec(pe->globalPos());
}

void CFuzzyTableView::slotSwitchGraphsView()
{
	if (m_table != 0)
	{
		//QGraphicsScene scene(QRectF(-100, -100, 900, 100));
		//QGraphicsLineItem* pLineItem = scene.addLine(QLineF(10, 10, 80, 80), QPen(Qt::red, 2));
		//pLineItem->setFlags(QGraphicsItem::ItemIsMovable);
		//scene.addText("Hello, world!");
		m_pFuzzyRulesForm = new CFuzzyRulesForm(m_table);
		m_pFuzzyRulesForm->show();
	}
	else
	{
		QMessageBox mb(QMessageBox::Critical, "FuzzySheet", "The model is not ready to be processed in order to set the rules.\nPlease check if all the data is Ok.", QMessageBox::Ok, this);
		mb.show();
	}
}
